<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */

class AppController extends Controller {
	
	var $components = array('Auth', 'Acl', 'Session');
	
	function valida_mail() {
		if (count($this->params['pass']) != 2) {
			$this->redirect(array('controller' => 'pages', 'action' => 'display'));
		}
		$mail = Sanitize::paranoid ($this->params['pass'][0], array ('_', '@', '.', '-'));
		$code = Sanitize::paranoid ($this->params['pass'][1], array ('-', '.'));
		$db_code = $this->Usuario->field ('validacion', array ('mail' => $mail));
		if ($db_code == $code) {		
			$this->Usuario->updateAll (
				array ('validacion' => '\'' . $this->genPass (64) . '\'', 'activo' => '1'),
				array ('mail' => $mail)
			);
			$this->Session->setFlash(__('E-mail validado', true));
		} else {
			$this->Session->setFlash(__('E-mail no validado', true));
		}
		$this->redirect(array());
	}
	
	function inicializar_autenticacion() {
		$this->Auth->userModel = 'Usuario';
		$this->Auth->authorize = 'actions';
		$this->Auth->fields = array("username" => "mail", "password" => "pass");
		$this->Auth->userScope = array('Usuario.activo' => true);
		$this->Auth->loginAction = array('controller' => 'usuarios', 'action' => 'login');
		$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display');
		$this->Auth->loginRedirect = array('controller' => 'usuarios', 'action' => 'index');
	}
	
	function beforeFilter(){
		Configure::write('Config.language', 'spa');
		$this->inicializar_autenticacion();
		//$this->Auth->allowedActions = array('*');
		$this->Auth->allowedActions = array('display');
		$this->Auth->actionPath = 'controllers/';
	}
	
	/* function search(){
		$this->autoRender = false;
		$search = $this->data[$this->modelClass]['Buscar'];
		$cond ="";
		$i=0;
		foreach($this->{$this->modelClass}->_schema as $field => $value){
			//debug($field);
			if($i>0){
				$cond = $cond. " OR ";
			}
			$cond = $cond. " ".$this->modelClass.".".$field." ILIKE '%".$search."%' ";
			$i++;
		}
		$conditions = array('limit'=>4, 'conditions'=> $cond);
		$this->paginate = $conditions;
		$this->set(strtolower($this->name), $this->paginate());
		$this->render('index');
	} */
	
	//Generador de claves que usaremos para nuestro sistema de seguridad
	protected function genPass ($len, $uppercase = true, $lowercase = true, $numbers = true, $sym1 = true) {
		$up_dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$lo_dict = 'abcdefghijklmnopqrstuvwxyz';
		$nu_dict = '0123456789';
		$s1_dict = '.-';
 
		$pass_dict = (($uppercase) ? $up_dict : '') .
			(($lowercase) ? $lo_dict : '') .
			(($numbers) ? $nu_dict : '') .
			(($sym1) ? $s1_dict : '');
 
		$dict_size = strlen ($pass_dict);
 
		$pass = '';
		for ($i=0; $i<$len; $i++) {
			$pass .= $pass_dict[rand (0, $dict_size-1)];
		}
 
		return $pass;
	}
	
}
